package com.itcast.test;

import com.itcast.dao.CutomerDao.CustomerDao;
import com.itcast.domain.Customer.Customer;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.annotation.Rollback;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;

//声明Spring中提供的单元测试
@RunWith(SpringJUnit4ClassRunner.class)
//加载配置文件信息
@ContextConfiguration(locations = "classpath:applicationContext.xml")
public class CustomerJPQLTset {
    @Autowired
    public CustomerDao customerDao;

    /**
     * 指定ID查询
     */
    @Test
    @Transactional
    public void  findJqpl( ) {
        Customer customer = customerDao.findjqpl("北大");
        System.out.println(customer);
    }

    /**
     * 多条件查询
     */
    @Test
    @Transactional
    public void  findcustnameandcustid( ) {
        Customer customer = customerDao.findCustnameandCustId("清华",4L);
        System.out.println(customer);
    }

    /**
     * 更新
     */
    @Test
    @Transactional
    //是否回滚事务
    @Rollback(value = false)
    public void  update( ) {
       customerDao.updateCustomer(4L,"交大");
    }
    //测试sql查询
    @Test
    public void testFindSql() {
        List<Object[]> list = customerDao.findSql("传智播客%");
        for(Object [] obj : list) {
            System.out.println(Arrays.toString(obj));
        }
    }


    //测试方法命名规则的查询
    @Test
    public void testNaming() {
        Customer customer = customerDao.findByCustName("传智播客");
        System.out.println(customer);
    }


    //测试方法命名规则的查询
    @Test
    public void testFindByCustNameLike() {
        List<Customer> list = customerDao.findByCustNameLike("传智播客%");
        for (Customer customer : list) {
            System.out.println(customer);
        }
    }


    //测试方法命名规则的查询
    @Test
    public void testFindByCustNameLikeAndCustIndustry() {
        Customer customer = customerDao.findByCustNameLikeAndCustIndustry("传智播客1%", "it教育");
        System.out.println(customer);
    }

}